package com.webSite.demo.cy.config;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

//@Configuration
public class DruidConfig {

	/**
	 * 绑定数据源
	* @Title: druid  
	* @param @return    参数  
	* @return DataSource    返回类型  
	* @throws
	 */
	@ConfigurationProperties(prefix="spring.datasource")
	@Bean
	public DataSource druid() {
		return new DruidDataSource();
	}
	
	@Bean
	public ServletRegistrationBean<StatViewServlet> statviewServlet(){
		ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<StatViewServlet>(new StatViewServlet());
		Map<String, String> initParams = new HashMap<String, String>();
		initParams.put("loginUsername", "admin");
		initParams.put("loginPassword", "123456");
		initParams.put("allow", "");//默认允许所有访问
		//initParams.put("deny", "120.24.90.162");//IP黑名单
		
		bean.setInitParameters(initParams);
		bean.addUrlMappings("/druid/*");
		return bean;
	}
	
	@Bean
	public FilterRegistrationBean<WebStatFilter> webStatFilter(){
		FilterRegistrationBean<WebStatFilter> registration = new FilterRegistrationBean<WebStatFilter>();//new myFilter()
		registration.addUrlPatterns("/*");
		registration.setFilter(new WebStatFilter());
		//设置初始化参数
		Map<String, String> initParams = new HashMap<String, String>();
		initParams.put("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
		registration.setInitParameters(initParams);
        return registration;
	}
}
